﻿using System.IO;
using UIFramwork.Util;
using UnityEditor;
using UnityEngine;

public class ScriptableObjectUtil
{

    /// <summary>
    /// 创建 | ScriptableObject
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path"></param>
    /// <param name="isOverride"></param>
    /// <returns></returns>
    public static T Create<T>(string projectPath) where T : ScriptableObject
    {
        UnitFileUtil.CreateFolder($"{Application.dataPath}/{projectPath}");

        Debug.Log($"{Application.dataPath}/{projectPath}");

        string path = EditorUtility.SaveFilePanelInProject("创建资产", $"{typeof(T).Name}", "asset", "选择你的保存目录", $"Assets/{projectPath}");


        bool isEmpty = string.IsNullOrEmpty(path);
        if (isEmpty)
        {
            Debug.LogWarning($"ScriptableObjectUtil | 资产创建失败！| {typeof(T).Name}");
            return null;
        }

        path = path.Trim();



        T @new = ScriptableObject.CreateInstance<T>();

        path = path.Replace(Application.dataPath, "Assets");
        AssetDatabase.CreateAsset(@new, path);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        return @new;
    }
}